package com.carson.undergo.ui.activity.home

import android.annotation.SuppressLint
import android.content.Context
import android.view.inputmethod.InputMethodManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.carson.undergo.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.model.common.base.activity.BaseActivity
import com.model.common.databinding.UiRefreshRecyclerListBinding
import com.model.common.utils.always.StringUtils
import com.model.common.utils.always.ToastUtils
import com.model.common.utils.view.weight.dialog.NormalDialog

/**
 * Create by Carson on 2022/1/4.
 * 弹框样式展示
 */
class DialogShowUI : BaseActivity<UiRefreshRecyclerListBinding>() {

    private val infoList = ArrayList<String>()
    private lateinit var adapter: BaseQuickAdapter<String, BaseViewHolder>

    override fun getViewBinding(): UiRefreshRecyclerListBinding =
        UiRefreshRecyclerListBinding.inflate(layoutInflater)

    override fun initViewsAndEvents() {
        initAdapter()
    }

    @SuppressLint("InflateParams")
    private fun initAdapter() {
        infoList.add("单个确定按钮dialog")
        infoList.add("确定取消dialog")
        infoList.add("单个输入框dialog")
        val layoutManager = LinearLayoutManager(mContext)
        layoutManager.isSmoothScrollbarEnabled = true
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        adapter = object : BaseQuickAdapter<String, BaseViewHolder>
            (R.layout.item_home_fragment, infoList) {
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.tv_content, item)
            }
        }
        binding.recyclerView.adapter = adapter
        //添加空视图
        val viewNull = layoutInflater.inflate(R.layout.root_nodata_empty, null)
        adapter.setEmptyView(viewNull)

        //操作
        adapter.setOnItemClickListener { _, _, position ->
            when (position) {
                //单个确定按钮dialog
                0 -> {
                    NormalDialog.Builder()
                        .setDialogType(NormalDialog.DIALOG_CONFIRM)
                        .setTitle("hello")
                        .setTip("单个确定按钮dialog")
                        .setPositiveListener { _, _ ->
                            ToastUtils.showMessage(mContext, "单个确定按钮dialog")
                        }
                        .build(mContext)
                        .show()
                }
                //确定取消dialog
                1 -> {
                    NormalDialog.Builder()
                        .setDialogType(NormalDialog.DIALOG_SELECT)
                        .setTitle("hello")
                        .setTip("确定取消dialog")
                        .setPositiveListener { _, _ ->
                            ToastUtils.showMessage(mContext, "确定后可以操作")
                        }
                        .build(mContext)
                        .show()
                }
                //单个输入框dialog
                2 -> {
                    NormalDialog.Builder()
                        .setDialogType(NormalDialog.DIALOG_ONE_INPUT)
                        .setTitle("hello")
                        .setTip("单个输入框dialog")
                        .setPositiveListener { dialog, _ ->
                            val content = dialog.inputText
                            if (StringUtils.isEmpty(content)) {
                                ToastUtils.showMessage(mContext, "请输入内容")
                            } else {
                                ToastUtils.showMessage(mContext, content)
                            }
                            //关闭软键盘
                            val imm =
                                dialog.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                            imm.hideSoftInputFromWindow(
                                dialog.window?.decorView?.windowToken,
                                0
                            )

                        }
                        .build(mContext)
                        .show()
                }
            }
        }
    }
}